package com.frostmourne.mxsguimaker.listeners;

import com.frostmourne.mxsguimaker.objects.GuiMakerHolder;
import com.frostmourne.mxsguimaker.objects.GuiMakerMold;
import com.frostmourne.mxsguimaker.objects.PlayerData;
import com.gitee.mxstarsex.objects.builders.bukkit.inventory.InventoryBuilder;
import com.gitee.mxstarsex.objects.builders.bukkit.item.ItemBuilder;
import com.gitee.mxstarsex.utils.CollectionUtils;
import com.gitee.mxstarsex.utils.DataTypeUtils;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.inventory.InventoryOpenEvent;
import org.bukkit.inventory.ItemStack;

import java.util.HashMap;
import java.util.Map;

public class InventoryListener implements Listener {

    @EventHandler
    public void open(InventoryOpenEvent e){
        if (e.getView().getTopInventory() != null
        && e.getView().getTopInventory().getHolder() != null &&
        e.getView().getTopInventory().getHolder() instanceof GuiMakerHolder){

            GuiMakerMold guiMakerMold = GuiMakerMold.getGuiMakerMold(e.getView().getTopInventory());
            if (guiMakerMold.getScript("open") != null){
                guiMakerMold.invokeIconScripts("open","main",new HashMap<>(),e,(Player)e.getPlayer());
            }

        }
    }

    @EventHandler
    public void close(InventoryCloseEvent e){

        if (e.getInventory() != null && e.getInventory().getHolder() != null
        && e.getInventory().getHolder() instanceof GuiMakerHolder){



            GuiMakerMold guiMakerMold = GuiMakerMold.getGuiMakerMold(e.getInventory());

            if (guiMakerMold.isSaveItems()){

                InventoryBuilder builder = guiMakerMold.getInventoryBuilder();

                Map<Integer,ItemStack> itemStackMap = new HashMap<>();

                for (int i = 0; i < e.getInventory().getSize(); i++) {
                    ItemStack item = e.getInventory().getItem(i);
                    if ( item != null ){
                        if (!builder.getItemBySlot(i).isSimilar(item)){
                            itemStackMap.put(i,item);
                        }
                    }
                }

                PlayerData data = PlayerData.getPlayerData((Player) e.getPlayer());
                data.setItems(guiMakerMold.getName(), itemStackMap);

            }

            if (guiMakerMold.getScript("close") != null){
                guiMakerMold.invokeIconScripts("close","main",new HashMap<>(),e,(Player)e.getPlayer());
            }


        }

    }

    @EventHandler
    public void click(InventoryClickEvent e){

        if (
                DataTypeUtils.notNull(
                e.getView(),e.getView().getTopInventory(),e.getView().getTopInventory().getHolder()
                  )
        ){


            if (e.getView().getTopInventory().getHolder() instanceof GuiMakerHolder){

                GuiMakerMold guiMakerMold = GuiMakerMold.getGuiMakerMold(e.getView().getTopInventory());

                if (e.getClickedInventory() == e.getWhoClicked().getInventory()){
                    if (!guiMakerMold.isClickSelf()){
                        e.setCancelled(true);
                    }
                    return;
                }

                int rawSlot = e.getRawSlot();
                for (String s : guiMakerMold.getInventoryBuilder().getSlotsMap().keySet()){
                    if (guiMakerMold.getInventoryBuilder().getSlotByKey(s).contains(rawSlot)){
                        HashMap<String, Object> objectObjectHashMap = new HashMap<>();
                        objectObjectHashMap.put("clickItem",new ItemBuilder(e.getCurrentItem()));
                        objectObjectHashMap.put("handItem",new ItemBuilder(e.getCursor()));

                        guiMakerMold.invokeIconScripts("click-" + s.toLowerCase(),"main",objectObjectHashMap,e,e.getWhoClicked());

                        break;
                    }
                }

            }

        }

//        if (e.getClickedInventory() != e.getWhoClicked().getInventory()){
//
//            int rawSlot = e.getRawSlot();
//            int i = Double.valueOf(106 / 9.0).intValue();
//            i = i + 1;
//            System.out.println("i = " + i);
//            System.out.println("i % 6 = " + (i % 6));
//            if (i % 6 == 0){
//                e.getWhoClicked().sendMessage("is slot");
//            }
//
//        }

    }

}
